#!/bin/bash

#------------------------------------------------------------------------------
# Author:  Marelie Davel (marelie.davel@gmail.com)
#------------------------------------------------------------------------------

srcdir=.
use_opt=1

if [ $# -ne 6 ]; then
  echo "Usage: extract_rules.sh <in:defref-patterns> <in:defref-classes> <out:defref-rules> <out:defref-gnulls> <out:defref-feats> <out:log>"

else
  patts=$1
  classes=$2
  rules=$3
  gnulls=$4
  feats=$5
  log=$6

  cut -f3 -d\; < $patts | sed 's/\(.\)/\1 /g' | tr ' ' '\012' | sort -u | sed -e '/^$/d' -e '/-/d' > $feats
  if [ $use_opt == 1 ]; then
    sed 's/.*;\(.*\);/\1; /' $patts > $patts.opt
    perl $srcdir/map_diacritic.pl opt-patts to-num $patts.opt $srcdir/map.graphs $patts.opt.mapped > $log
    defaultrefine extract d $patts.opt.mapped $rules.no-diac > $log
  else
    echo "TODO: this selection not as well tested as use_opt. First check!"
    perl $g2pdir/defref.pl rules_single d $patts $rules > $log 
  fi
  perl $srcdir/map_diacritic.pl rules to-char $rules.no-diac $srcdir/map.graphs $rules > $log
fi

